package com.czy.owner.ui.caseshow;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.CaseShowAdapter;
import com.czy.owner.api.CaseShowApi;
import com.czy.owner.callback.RecycleViewDivider;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.CaseShowModel;
import com.czy.owner.entity.CouponsCountModel;
import com.czy.owner.entity.FindCarInfoModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.api.BaseResultEntity;
import com.czy.owner.net.http.HttpManager;
import com.czy.owner.net.listener.HttpOnNextListener;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.information.InformationDetailActivity;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.easyrecycleview.EasyRecyclerView;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangyang on 2017/5/19 0019.
 * 案例展示
 */

public class CaseShowActivity extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.easy_recycleview)
    EasyRecyclerView recyclerView;
    private int page = 1;
    private Handler handler = new Handler();
    private CaseShowAdapter adapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.common_activity_easyrecycleview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.store_case_show);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapterWithProgress(adapter = new CaseShowAdapter(this));
        recyclerView.addItemDecoration(new RecycleViewDivider(CaseShowActivity.this, LinearLayoutManager.VERTICAL, R.drawable.shape_recyclerview_divider_decoration));
        recyclerView.setErrorView(R.layout.view_error_net);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                // adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });

        adapter.setError(R.layout.view_error_net, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();

            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();

            }
        });
        recyclerView.setRefreshListener(this);

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener<CaseShowModel.MyList>() {
            @Override
            public void onItemClick(View view, int position, CaseShowModel.MyList model) {
                if (PhoneUtils.isNetworkConnected(CaseShowActivity.this)) {
                    Intent ti = new Intent(CaseShowActivity.this, InformationDetailActivity.class);
                    ti.putExtra("title", model.getTitle());
                    ti.putExtra("content", model.getContent());
                    ti.putExtra("infoId", model.getInfoId());
                    ti.putExtra("collectType", "caseShow");
                    try {
                        if (model.getResList().size() > 0) {
                            ti.putExtra("imgUrl", model.getResList().get(0).getThumbnailUrl());
                        }
                    } catch (Exception e) {

                    }

                    startActivity(ti);
                }

            }
        });

    }

    @Override
    protected void updateViews() {

    }


    private void getDataList() {
        CaseShowApi caseShowApi = new CaseShowApi(new HttpOnNextListener<CaseShowModel>() {
            @Override
            public void onNext(CaseShowModel model) {
                setDataList(model.getList());
            }

            @Override
            public void onCacheNext(String cache) {
                super.onCacheNext(cache);
                Gson gson = new Gson();
                Type type = new TypeToken<BaseResultEntity<CaseShowModel>>() {
                }.getType();
                BaseResultEntity<CaseShowModel> cacheList = gson.fromJson(cache, type);
                setDataList(cacheList.getData().getList());
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                PhoneUtils.ShowToastMessage(CaseShowActivity.this, e.getMessage());
            }
        }, this, page + "");
        caseShowApi.setSession(UserHelper.getInstance().getSessionInfoModel(CaseShowActivity.this).getSession());
        caseShowApi.setStoreId(SharedPreferencesUtils.getParam(CaseShowActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
        caseShowApi.setPage(page + "");
        caseShowApi.setSize("10");
        HttpManager httpManager = HttpManager.getInstance();
        httpManager.doHttpDeal(caseShowApi);
    }

    private void setDataList(List<CaseShowModel.MyList> list) {
        if (list.size() < 9) {
            adapter.stopMore();
        }
        adapter.addAll(list);
        if (adapter.getAllData().size() == 0) {
            recyclerView.setEmptyView(R.layout.view_empty_common);
            recyclerView.showEmpty();
        }
    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //刷新
                if (!PhoneUtils.isNetworkConnected(CaseShowActivity.this)) {
                    adapter.pauseMore();
                    recyclerView.showError();
                    return;
                }
                page++;
                getDataList();

            }
        }, 1000);
    }

    @Override
    public void onRefresh() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                adapter.clear();
                page = 1;
                getDataList();
            }
        }, 1000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        onRefresh();
    }
}
